package communication.login
{
	import modules.GameDispatcher;
	import modules.login.Login;
	import modules.login.LoginEvent;
	
	import protobuf.LG_PKG_LOGIN_ACK;
	import protobuf.PKG_AUTH_CHALLENGE_CMD;
	import protobuf.LG_PKG_LOGIN_ACK.E_RESULT_TYPE;


	public final class LoginMsgProcesser
	{


		private static var _instance:LoginMsgProcesser;

		public static function getInstance():LoginMsgProcesser
		{
			if (_instance == null)
			{
				_instance = new LoginMsgProcesser();
			}
			return _instance;
		}

		private var gameDispatcher:GameDispatcher = GameDispatcher.instance;

		public function OnAuthChallengeCmd(pkg:PKG_AUTH_CHALLENGE_CMD):void
		{
		}

		public function OnLoginAck(pkg:LG_PKG_LOGIN_ACK):void
		{
			Login.logining = false;
			
			var result:int = pkg.result;
			logger("登陆结果: " + result);
			switch (result)
			{
				case E_RESULT_TYPE.eSUCCESS:
					logger("登陆成功.");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_SUCCEED,pkg));
					Login.logined = true;
					break;
				case E_RESULT_TYPE.eINVAILD_TOKEN:
					logger("token错误");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_FAIL));
					break;
				case E_RESULT_TYPE.eINVAILD_PASSWORD:
					logger("用户名或密码错误");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_FAIL, {info:"用户名或密码错误！"}));
					break;
				case E_RESULT_TYPE.eINNER_SERVER_ERROR:
					logger("服务器错误错误");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_FAIL, pkg));
					break;
				case E_RESULT_TYPE.eINVAILD_OLD_CERT:
					logger("验证错误");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_FAIL, pkg));
					break;
				default:
					logger("登陆失败");
					gameDispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_FAIL, pkg));
					break;
			}
		}
	}
}
